package com.zyd.shiro.controller.admin;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;

import okhttp3.MultipartBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import com.zyd.shiro.persistence.beans.WxappHelp;
import com.zyd.shiro.business.service.MallWxappHelpService;

import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.util.ResultUtil;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器层
 * @author RookieWZW
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/wxappHelp")
public class WxappHelpController {

	@Autowired
	private MallWxappHelpService wxappHelpService;

	/**test
	 *
	 */
	@RequestMapping(value = "/ttt/",method=RequestMethod.GET)
	public ModelAndView page(){
		return ResultUtil.view("addKnowlege");
	}
	/**
	 * 查询全部数据，已测试
	 * @return
	 */
	@RequestMapping(value="/findAll/",method=RequestMethod.GET)
	public ResponseVO findAll(){
		return ResultUtil.success("查询成功",wxappHelpService.findAll());

	}
	/**
	 * 按时间顺序查询小知识，新的小知识在后面，旧的小知识在前面，已测试
	 */
	@RequestMapping(value="/findAllCreateTimeASC/",method=RequestMethod.GET)
	public ResponseVO findAllCreateTimeASC(){
		return ResultUtil.success("查询成功",wxappHelpService.findAllHelpASC());

	}
	/**
	 * 按时间逆序查询小知识，旧的小知识在后面，新的小知识在前面，已测试（应默认使用这个)
	 */
	@RequestMapping(value="/findAllCreateTimeDESC/",method=RequestMethod.GET)
	public ResponseVO findAllCreateTimeDESC(){

		return ResultUtil.success("查询成功",wxappHelpService.findAllHelpDESC());
	}
	/**
	 * 按标题查找,时间顺序，已测试
	 */
	@RequestMapping(value="/findAllTitleASC/{title}",method=RequestMethod.GET)
	public ResponseVO findAllTitleASC(@PathVariable String title){
		return ResultUtil.success("查询成功",wxappHelpService.findAllByTitleASC(title));
	}
	/**
	 * 按标题查找,时间逆序，已测试
	 */
	@RequestMapping(value="/findAllTitleDESC/{title}",method=RequestMethod.GET)
	public ResponseVO findAllTitleDESC(@PathVariable String title){
		return ResultUtil.success("查询成功",wxappHelpService.findAllByTitleDESC(title));
	}



	/**
	 * 根据ID查询，已测试
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/findOneById/{id}",method=RequestMethod.GET)
	public ResponseVO findOne(@PathVariable Integer id){
		return ResultUtil.success("查询成功", wxappHelpService.findOne(id));

	}

	/**
	 * 获得图片显示图片网址
	 * @
	 * @
	 */
	@RequestMapping(value="/getImage/{id}",method=RequestMethod.GET)
	public ResponseVO getImage(@PathVariable Integer id){
		WxappHelp wxappHelp = wxappHelpService.findOne(id);
		try {
			return ResultUtil.success("查询成功", wxappHelpService.downloadTest(wxappHelp.getTitle_image()));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return ResultUtil.error("未找到图片");
		}
	}
	/**
	 *
	 */
	/**
	 * 分页查询全部数据
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/{page}/{size}",method=RequestMethod.GET)
	public ResponseVO findPage(@PathVariable int page,@PathVariable int size){

		Page<WxappHelp> pageList = wxappHelpService.findPage(page, size);
		return ResultUtil.success("查询成功", new PageResult(pageList.getTotalElements(), pageList.getContent()) );
	}

	/**
	 * 分页+多条件查询 
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/{page}/{size}/{searchMap}/{type}",method=RequestMethod.POST)
	public ResponseVO findSearch(@PathVariable String searchMap ,@PathVariable int type ,@PathVariable int page,@PathVariable int size){

		Page<WxappHelp> pageList = wxappHelpService.findSearch(searchMap,type, page, size);
		return ResultUtil.success("查询成功",  new PageResult(pageList.getTotalElements(), pageList.getContent()) );
	}
	@RequestMapping(value="/findAllByType/{page}/{size}/{type}",method=RequestMethod.POST)
	public ResponseVO findSearch(@PathVariable int type ,@PathVariable int page,@PathVariable int size){

		Page<WxappHelp> pageList = wxappHelpService.findAllSearchByType(type, page, size);
		return ResultUtil.success("查询成功",  new PageResult(pageList.getTotalElements(), pageList.getContent()) );
	}

	/**
	 * 增加,已测试
	 * @param wxappHelp
	 */
	@RequestMapping(value="/add/",method=RequestMethod.POST)
	public ResponseVO add( WxappHelp wxappHelp, @RequestPart("file") MultipartFile file){

		String path = wxappHelpService.uploadTest(file);
		wxappHelp.setTitle_image(path);
		wxappHelp.setCreateTime(new Date());
		wxappHelpService.add(wxappHelp);
		return ResultUtil.success("增加成功");
	}

	/**
	 * 修改，已测试
	 * @param wxappHelp
	 */
	@RequestMapping(value="/update/",method=RequestMethod.POST)
	public ResponseVO update(WxappHelp wxappHelp,MultipartFile file){

		WxappHelp newwx = wxappHelpService.findOne(wxappHelp.getId());
		if(file!=null)
		{
			String path = wxappHelpService.uploadTest(file);
			wxappHelp.setTitle_image(path);
			wxappHelp.setCreateTime(newwx.getCreateTime());
		}else{
			wxappHelp.setTitle_image(newwx.getTitle_image());
			wxappHelp.setCreateTime(newwx.getCreateTime());
		}
		wxappHelp.setUpdate_time(new Date());
		wxappHelpService.update(wxappHelp);
		return ResultUtil.success("修改成功");
	}

	/**
	 * 删除，已测试
	 * @param id
	 */
	@RequestMapping(value="/delete/{id}",method=RequestMethod.DELETE)
	public ResponseVO delete(@PathVariable Integer id ){
		wxappHelpService.delete(id);
		return ResultUtil.success("删除成功");
	}

}
